﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace L9SubSet.lwf
{
    // 端口挂载的灯具信息
    public class PortTubeData
    {
        public const int MAX_TUBE_NUMBER_PER_PORT = 895;	// 端口最大灯具数量

        public int TubeColor;			// 灯点的颜色
        public int WireColor;			// 连线的颜色
        
		public Rectangle MaxRect;		// 灯点最大的范围，小于或者等于定义的像素点
        
		public List<Point> TubePoint;	// 存储对应的灯点数
        public List<int> TubeMode;		// 存储对应的灯点数

		public int ColorType = 1;		// 端口颜色列表, 默认为rgb

		// 构造函数
        public PortTubeData()
        {
            TubePoint = new List<Point>();
			TubeMode = new List<int>();
			Clear();
        }

		// 清除端口数据
        public int Clear()
        {
            TubeColor = Color.White.ToArgb();
            WireColor = Color.Green.ToArgb();
            
			MaxRect.X = 0;
            MaxRect.Y = 0;
            MaxRect.Width = 0;
            MaxRect.Height = 0;
            
			TubeMode.Clear();
            TubePoint.Clear();
            
			return 0;
        }

		// 复制端口数据
        public PortTubeData Copy()
        {
            PortTubeData ttt = new PortTubeData();
            
			ttt.TubeColor = TubeColor;
            ttt.WireColor = WireColor;
            ttt.MaxRect = new Rectangle(MaxRect.Left, MaxRect.Top, MaxRect.Right, MaxRect.Bottom);
			ttt.ColorType = ColorType;
            
			for (int i = 0; i < TubePoint.Count(); i++)
            {
                Point ppp = new Point(TubePoint[i].X, TubePoint[i].Y);
                ttt.TubePoint.Add(ppp);
            }
           
			for (int i = 0; i < TubeMode.Count(); i++)
            {
                ttt.TubeMode.Add(TubeMode[i]);
            }
            
			return ttt;
        }

		// 端口灯具数量
        public int TubeMax()
        {
            return TubePoint.Count;
        }

		// 添加灯具
        public int AddTube(Point ppp)
        {
            if (TubePoint.Count > MAX_TUBE_NUMBER_PER_PORT)
            {
                return 0;
            }

            TubePoint.Add(ppp);
            TubeMode.Add(0);

            if (TubeMax() == 1)
            {
                MaxRect.X = ppp.X;
                MaxRect.Y = ppp.Y;
                MaxRect.Width = 1;
                MaxRect.Height = 1;
            }
            else
            {
				if (ppp.X >= MaxRect.Right)
				{
					MaxRect.Width = ppp.X - MaxRect.X + 1;
				}
				else if (ppp.X < MaxRect.X)
				{
					MaxRect.Width = MaxRect.Right - ppp.X;
					MaxRect.X = ppp.X;
				}

				if (ppp.Y >= MaxRect.Bottom)
				{
					MaxRect.Height = ppp.Y - MaxRect.Y + 1;
				}
				else if (ppp.Y < MaxRect.Y)
				{
					MaxRect.Height = MaxRect.Bottom - ppp.Y;
					MaxRect.Y = ppp.Y;
				}
            }

            return 0;
        }

		// 删除灯具
        public int DeleteTubeAt(int index)
        {
            TubePoint.RemoveAt(index);
			TubeMode.RemoveAt(index);
			return 0;
        }

		// 插入灯具
        public int InsertTubeAt(int index, Point ppp)
        {
            TubePoint.Insert(index, ppp);
			TubeMode.Insert(index, 0);
			return 0;
        }

		// 定位灯具
        public int FindTube(Point ppp)
        {
            int i;
			for (i = 0; i < TubeMax(); i++)
			{
				if (ppp == TubePoint[i])
				{
					return i;
				}
			}
			return -1;
        }

		// 移动灯具
        public int MoveTube(int index, Point ppp)
        {
            Point sss = TubePoint[index];
            sss.Offset(ppp);
            
			TubePoint.RemoveAt(index);
            TubePoint.Insert(index, sss);

            return 0;
        }

		// 定位灯具
        public int FindTube(Point ppp, int control, int port, List<TubeData> ttt)
        {
            int s_num = 0;
			for (int i = 0; i < TubeMax(); i++)
			{
				if (ppp == TubePoint[i])
				{
					TubeData aaa = new TubeData(control, port, i);
					ttt.Add(aaa);

					if (TubeMode[i] == 1)
					{
						s_num++;
					}
				}
			}
            return s_num;
        }

		// 选择灯具
        public int SeleteTubeAt(int index, int select)
        {
            TubeMode[index] = select;
			return 0;
        }

		// 按端口选择所有灯具
        public int SeleteTubePort(int select)
        {
			for (int i = 0; i < TubeMax(); i++)
			{
				TubeMode[i] = select;
			}
			return 0;
        }

		// 按区域选择灯具
        public int SeleteTubeArea(Rectangle s_area, int ccc, int ppp, List<TubeData> ttt)
        {
			for (int i = 0; i < TubeMax(); i++)
			{
				if (s_area.Contains(TubePoint[i]))
				{
					if (TubeMode[i] == 0)
						ttt.Add(new TubeData(ccc, ppp, i));

					TubeMode[i] = 1;
				}
			}
            return 0;
        }

		// 清除选择的灯具
        public int SelectClear()
        {
			for (int i = 0; i < TubeMax(); i++)
			{
				TubeMode[i] = 0;
			}
			return 0;
        }

        // 设置灯点颜色
        public int SetTubeColor(int color)
        {
            TubeColor = color;
			return 0;
        }

        // 设置连线颜色
        public int SetWireColor(int color)
        {
            WireColor = color;
			return 0;
        }

		// 比较端口灯具数据
        public Boolean ComPortData(PortTubeData portdata)
        {
            // 判断灯点颜色和灯点连线颜色是否有变化
            if (TubeColor != portdata.TubeColor || WireColor != portdata.WireColor)
            {
                return true;
            }
            
			// 判断此端口的灯点数是否有变化
            if (TubePoint.Count != portdata.TubePoint.Count)
            {
                return true;
            }
            
			// 查看所有灯点坐标是否有变化
            for (int i = 0; i < TubePoint.Count; i++)
            {
                if (TubePoint[i].X != portdata.TubePoint[i].X || TubePoint[i].Y != portdata.TubePoint[i].Y)
                {
                    return true;
                }
            }
            
			// 判断所属颜色列表项是否有变化
            //if (ColorType != portdata.ColorType)
            //{
            //    return true;
            //}

			return false;
        }
    }
}
   